<?php 
	namespace app\admin\controller;
	use think\Controller;
	use think\Session;
	use think\Db;


	//项目管理
	class Pro extends Allow
	{   
        //项目列表
        public function getIndex()
		{
			//创建请求对象
            $request=request();
            $a=$request->param('palias');
            $t=$request->param('ptype');
            $s=$request->param('sec');
            $p=$request->param('phone');
            $m=$request->param('mail');
            $ps=$request->param('pstatus');

            //分配数据给列表
	        $data=Db::table("pro")->where('palias',"like","%".$a."%")->where('ptype',"like","%".$t."%")->where('sec',"like","%".$s."%")->where('phone',"like","%".$p."%")->where('mail',"like","%".$m."%")->where('pstatus',"like","%".$ps."%")->paginate(10);
			return $this->fetch('pro/index',['data'=>$data,'request'=>$request]);
		}

        //加载添加页面
		public function getAdd()
		{

			//读取set表电话邮箱数据
	        $set=Db::table("set")->order('sid','desc')->limit(1)->select();
            
            //获取数据拆分成想要的格式
            $notifyphone=$set[0]['notifyphone'];
            $phone=explode(",",$notifyphone);
            $notifymail=$set['0']['notifymail'];
            $mail =explode(",",$notifymail);

            //查询项目分类表分配数据
            $pro=Db::table("protype")->select();
			return $this->fetch('pro/add',['phone'=>$phone,'mail'=>$mail,'pro'=>$pro]);
		}

        //执行添加 
		public function postInsert()
		{
	       	//获取请求数据
            $request=request();
            $data=$request->only(['pid','palias','ptype','pname','sec','ptime','pstatus']);


            //将选中的值拼接成字符串
            $notifyphone=$request->only('phone');
            $phone=implode(",",$notifyphone);
            $notifymail=$request->only('mail');
            $mail =implode(",",$notifymail);

            if($data['pstatus']=='0'){
                $data['pstatus']='启用';
            }else{
                $data['pstatus']='禁用';
            }

            //准备数据
            $data['phone']=$phone;
            $data['mail']=$mail;
            $data['ptime']=date('Y-m-d H:i:s');

            //判断是否插入成功
            if(Db::table('pro')->insert($data)){
                $this->success("数据添加成功","/pro/index");
            }else{
                $this->contentor("数据插入失败","/pro/add");
            }

		}

 
        //加载修改模板
		public function getEdit()
		{
            //创建请求对象
            $request=request();
            //获取id
            $id=$request->param('pid');
            //获取需要修改的数据
            $data=Db::table("pro")->where("pid",$id)->find();
            //读取set表电话邮箱数据
            $set=Db::table("set")->order('sid','desc')->limit(1)->select();
            $notifyphone=$set[0]['notifyphone'];
            $phone=explode(",",$notifyphone);
            $notifymail=$set['0']['notifymail'];
            $mail =explode(",",$notifymail);
            //查询项目分类表
            $pro=Db::table("protype")->select();
            foreach($pro as $key=>$pros){
            }
            return $this->fetch('pro/edit',['data'=>$data,'phone'=>$phone,'mail'=>$mail,'pro'=>$pro]);

		}


	    //执行修改
        public function postUpdate()
        {
            //获取请求数据
        	$request=request();
            $id=$request->param('pid');
            $d=Db::table("pro")->where("pid",$id)->find();
            //获取字段信息
            $data=$request->only(['pid','palias','pname','ptype','sec','ptime','pstatus']);
            $notifyphone=$request->only('phone');
            $phone=implode(",",$notifyphone);
            $notifymail=$request->only('mail');
            $mail =implode(",",$notifymail);

            if($data['pstatus']=='0'){
                $data['pstatus']='启用';
            }
            if($data['pstatus']=='1'){
                $data['pstatus']='禁用';
            }
            //设置time字段时间值
            $data['ptime']=date('Y-m-d H:i:s');
            //查询设置表
            $set=Db::table("set")->select();
            $data['phone']=$set['0']['notifyphone'];
            $data['mail']=$set['0']['notifymail'];
            $data['ptime']=date('Y-m-d H:i:s');
            //判断是否修改成功
        	if(Db::table('pro')->where('pid',"{$id}")->update($data))
        	{
        		$this->success('修改成功','/pro/index');
        	}else{
        		$this->error('修改失败','/pro/edit/pid/'.$id);
        	}
        }
        
        //执行删除
    	public function getDel()
		{
	       	//创建请求对象
            $request=request();
           	$id=$request->param('pid');

            //判断是否删除成功
    	    if(Db::table('pro')->where('pid',"{$id}")->delete())
    	    {
    	    	$this->success('删除成功','/pro/index');
    	    }else{
    	    	$this->error('删除失败','/pro/index');
    	    }
            
		}
	}
 ?>